"""Git Log Import Tool main module.

@Project :   git_log_import
@File    :   main.py
@Time    :   2025/08/03 10:00:00
@Author  :   Jimmy
@Version :   1.0
@Desc    :   主函数
"""

import argparse
import datetime

from .api.db_operator import DBOperator
from .api.git_reader import GitReader
from .service.account_service import AccountService
from .service.commit_log_service import CommitLogService
from .service.repository_service import RepositoryService


def main():
    """Main function."""
    parser = argparse.ArgumentParser(description="Git Log Import Tool")
    parser.add_argument("command", choices=["import"], help="要执行的命令")
    parser.add_argument(
        "--type",
        choices=["accounts", "repositories", "commits"],
        required=True,
        help="导入类型",
    )
    parser.add_argument(
        "--instance",
        choices=["beijing", "chengdu"],
        help="commits 导入的实例（仅对 commits 有效）",
    )
    parser.add_argument("--since", help="commits 最迟还在活跃的仓库 (格式: YYYY-MM-DD)")
    parser.add_argument("--start", help="commits 导入的起始日期 (格式: YYYY-MM-DD)")
    parser.add_argument("--end", help="commits 导入的结束日期 (格式: YYYY-MM-DD)")
    args = parser.parse_args()

    # 初始化 GitReader 和 DBOperator（假设构造函数）
    git_reader = GitReader(args.instance)
    db_operator = DBOperator()

    if args.type == "accounts":
        service = AccountService(git_reader, db_operator)
        service.start_import_accounts()
    elif args.type == "repositories":
        service = RepositoryService(git_reader, db_operator)
        service.start_import_repositories()
    elif args.type == "commits":
        if not args.instance:
            raise ValueError("commits 类型需要指定 --instance")
        if not args.since:
            raise ValueError("commits 类型需要指定 --since")
        if not args.start:
            raise ValueError("commits 类型需要指定 --start")
        if not args.end:
            raise ValueError("commits 类型需要指定 --end")
        since = datetime.datetime.strptime(args.since, "%Y-%m-%d")
        start = datetime.datetime.strptime(args.start, "%Y-%m-%d")
        end = datetime.datetime.strptime(args.end, "%Y-%m-%d")

        service = CommitLogService(git_reader, db_operator)
        service.start_import_commits(
            last_activity_after=since,
            log_start_date=start,
            log_end_date=end,
        )


if __name__ == "__main__":
    main()
